$.ajax({
    async: true,
    type: "GET",
    data: {"curr": 1, "proCode": $("#proCode").val(), "proName": $("#proName").val()},
    url: "/provider/list.html",
    success: function (data) {
        if (data != null) {

            $('#table tr:gt(0)').remove();
            data=$.parseJSON(data)
            for (let i = 0; i < data.length; i++) {
                console.log(data[i])
                var c = '<tr><td><span>' + data[i].proCode + '</span></td>\n' +
                    '            <td><span>' + data[i].proName + '</span></td>\n' +
                    '            <td><span>' + data[i].proContact + '</span></td>\n' +
                    '            <td><span>' + data[i].proPhone + '</span></td>\n' +
                    '            <td><span>' + data[i].proFax + '</span></td>\n' +
                    '            <td><span>' + formatDate(new Date(data[i].creationDate)) + '</span></td>\n' +
                    '            <td>\n' +
                    '                <span><a class="viewProvider" href="/provider/view.html?proid=' + data[i].id + '"><img src="/statics/images/read.png" alt="查看" title="查看"/></a></span>\n' +
                    '                <span><a class="modifyProvider" href="/provider/modify.html?proid=' + data[i].id + '"><img src="/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>\n' +
                    '                <span><a class="deleteProvider" onclick="return del()" href="/provider/delete.html?proid=' + data[i].id + '"><img src="/statics/images/schu.png" alt="删除" title="删除"/></a></span>\n' +
                    '            </td>\n' +
                    '        </tr>';
                $("#table").append(c);
            }
        }
        $.ajax({
            async: true,
            type: "GET",
            data: {"curr": 1, "proCode": $("#proCode").val(), "proName": $("#proName").val()},
            url: "/provider/page.html",
            success: function (pageInfo) {
                pageInfo=$.parseJSON(pageInfo);
                console.log(pageInfo)
                $("#total").text(pageInfo.totals);
                $("#curr").text(pageInfo.pageNum);
                $("#pages").text(pageInfo.pagess)
            }
        });
    }
});


function getData(curr) {
    $.ajax({
        async: true,
        type: "POST",
        data: {"curr": curr, "proCode": $("#proCode").val(), "proName": $("#proName").val()},
        url: "/provider/page.html",
        success: function (pageInfo) {
            if (pageInfo != null) {
                pageInfo=$.parseJSON(pageInfo);

                $("#total").text(pageInfo.totals);
                $("#curr").text(pageInfo.pageNum);
                $("#pages").text(pageInfo.pagess)
                $.ajax({
                    async: true,
                    type: "POST",
                    data: {"curr": curr, "proCode": $("#proCode").val(), "proName": $("#proName").val()},
                    url: "/provider/list.html",
                    success: function (data) {
                        if (data != null) {
                            $('#table tr:gt(0)').remove();
                            data=$.parseJSON(data)
                            for (let i = 0; i < data.length; i++) {
                                var c = '<tr><td><span>' + data[i].proCode + '</span></td>\n' +
                                    '            <td><span>' + data[i].proName + '</span></td>\n' +
                                    '            <td><span>' + data[i].proContact + '</span></td>\n' +
                                    '            <td><span>' + data[i].proPhone + '</span></td>\n' +
                                    '            <td><span>' + data[i].proFax + '</span></td>\n' +
                                    '            <td><span>' + formatDate(new Date(data[i].creationDate)) + '</span></td>\n' +
                                    '            <td>\n' +
                                    '                <span><a class="viewProvider" href="/provider/view.html?proid=' + data[i].id + '"><img src="/statics/images/read.png" alt="查看" title="查看"/></a></span>\n' +
                                    '                <span><a class="modifyProvider" href="/provider/modify.html?proid=' + data[i].id + '"><img src="/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>\n' +
                                    '                <span><a class="deleteProvider" onclick="return del()" href="/provider/delete.html?proid=' + data[i].id + '"><img src="/statics/images/schu.png" alt="删除" title="删除"/></a></span>\n' +
                                    '            </td>\n' +
                                    '        </tr>';
                                $("#table").append(c);
                            }
                        }
                    }
                });
            }
        }
    });

}

function next() {
    var curr = parseInt($("#curr").text());
    if (curr + 1 > parseInt($("#pages").text())) {
        alert("最后一页");
    } else {
        getData(curr + 1);
    }
}

function pre() {
    var curr = parseInt($("#curr").text());
    if (curr - 1 == 0) {
        alert("第一页");
    } else {
        getData(curr - 1);
    }
}

function lastPage() {
    getData(parseInt($("#pages").text()));
}

function jumpPage() {
    var curr = parseInt($("#inputPage").val());
    if (curr <= 0 || curr > parseInt($("#pages").text())) {
        alert("请输入正确的页数");
    } else {
        getData(curr);
    }
}

function search() {
    $.ajax({
        async: true,
        type: "POST",
        data: {"curr": 1, "proCode": $("#proCode").val(), "proName": $("#proName").val()},
        url: "/provider/page.html",
        success: function (pageInfo) {
            pageInfo=$.parseJSON(pageInfo)

            $("#total").text(pageInfo.totals);
            $("#curr").text(pageInfo.pageNum);
            $("#pages").text(pageInfo.pagess)
        }
    });
    getData(1);
}

function formatDate(now) {
    var year = now.getFullYear();  //取得4位数的年份
    var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
    var date = now.getDate();      //返回日期月份中的天数（1到31）
    return year + "-" + month + "-" + date;
}

function del() {
    if(confirm("确认删除")){
        return true;
    }else {
        return false;
    }
}
